<?php
/*
 * desc：
 * author：wh
 * email：
 * createTime：{2019/11/13} {19:00} 
 */

namespace libraries\systemlogs;

use think\Db;
use think\facade\Log;
use think\facade\Request;

/**
 * 系统异常日志
 * 【注意】： 如果错误被捕获，则无法记录自动日志,需要手动调用并记录！！！！！！！
 * Class SysExceptionLog
 * @package libraries\systemlogs
 */
class SysExceptionLog
{

    static function add($e){
        $url = Request::baseUrl();
        $message = $e->getMessage();
        $trace = $e->getTraceAsString();
        $domain = Request::host();
        $method = Request::method();
        $param = encodeJson(Request::param());

       self::exceptionMonitor($domain, $url, $method, $param, $message, $trace);
    }


    protected static function exceptionMonitor($domain, $url, $method, $param, $message, $trace){
        $data = [
            'domain'=>$domain?$domain:'',
            'url'=>$url,
            'method'=>$method,
            'param'=>$param,
            'title'=>$message,
            'ip'=>Request::ip(),
            'create_time'=>date('Y-m-d H:i:s'),
        ];
       try{
           $parent_id = Db::table('log_exception')->insertGetId($data);

           $data_info = ['parent_id'=>$parent_id,'content'=>$trace?$trace:''];

           Db::table('log_exception_info')->insert($data_info);
       }catch (\Exception $e){
            //DATABASE BOOM
           //tp6
           //Log::error('========[数据库异常:DATABASE BOOM]========'.$e->getTraceAsString());
           //Log::close();
           //tp5.1
           Log::write('========[DATABASE ERROR:DATABASE BOOM]========【'.$e->getMessage().'】'.$e->getTraceAsString());
       }
    }
}